package com.example.wxback.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.wxback.config.annotation.Logical;
import com.example.wxback.config.annotation.RequiresPermissions;
import com.example.wxback.model.UserInfo;
import com.example.wxback.service.UserService;
import com.example.wxback.service.WxUserService;
import com.example.wxback.util.CommonUtil;
import com.example.wxback.util.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author: heeexy
 * @description: 用户/角色/权限相关controller
 * @date: 2017/11/2 10:19
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private WxUserService wxUserService;

    /**
     * 查询用户列表
     */
    @RequiresPermissions("user:list")
    @GetMapping("/list")
    public JSONObject listUser(HttpServletRequest request) {
        return userService.listUser(CommonUtil.request2Json(request));
    }

    @RequiresPermissions("user:add")
    @PostMapping("/addUser")
    public JSONObject addUser(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "username, password, nickname, roleIds");
        return userService.addUser(requestJson);
    }

    @RequiresPermissions("user:update")
    @PostMapping("/updateUser")
    public JSONObject updateUser(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, " nickname, roleIds, deleteStatus, userId");
        return userService.updateUser(requestJson);
    }

    @RequiresPermissions(value = {"user:add", "user:update"}, logical = Logical.OR)
    @GetMapping("/getAllRoles")
    public JSONObject getAllRoles() {
        return userService.getAllRoles();
    }

    /**
     * 角色列表
     */
    @RequiresPermissions("role:list")
    @GetMapping("/listRole")
    public JSONObject listRole() {
        return userService.listRole();
    }

    /**
     * 查询所有权限, 给角色分配权限时调用
     */
    @RequiresPermissions("role:list")
    @GetMapping("/listAllPermission")
    public JSONObject listAllPermission() {
        return userService.listAllPermission();
    }

    /**
     * 新增角色
     */
    @RequiresPermissions("role:add")
    @PostMapping("/addRole")
    public JSONObject addRole(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "roleName,permissions");
        return userService.addRole(requestJson);
    }

    /**
     * 修改角色
     */
    @RequiresPermissions("role:update")
    @PostMapping("/updateRole")
    public JSONObject updateRole(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "roleId,roleName,permissions");
        return userService.updateRole(requestJson);
    }

    /**
     * 删除角色
     */
    @RequiresPermissions("role:delete")
    @PostMapping("/deleteRole")
    public JSONObject deleteRole(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "roleId");
        return userService.deleteRole(requestJson);
    }

    @PutMapping("/password")
    public JSONObject updatePassword(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "userId,oldPassword,newPassword,newPassword2");

        Integer userId = requestJson.getInteger("userId");
        String oldPassword = requestJson.getString("oldPassword");
        String newPassword = requestJson.getString("newPassword");
        String newPassword2 = requestJson.getString("newPassword2");

        UserInfo userInfo = wxUserService.getUser(userId);
        if (userInfo == null) {
            return CommonUtil.errorJson("用户不存在");
        } else if (!userInfo.getPassword().equals(oldPassword)) {
            return CommonUtil.errorJson("密码不正确");
        }else if (newPassword.equals(oldPassword)) {
            return CommonUtil.errorJson("新密码与旧密码不能一样");
        } else if (!newPassword.equals(newPassword2)) {
            return CommonUtil.errorJson("两次密码不一致");
        }
        String checkPassword = StringTools.checkPassword(newPassword);
        if (StringUtils.isNotBlank(checkPassword)){
            return CommonUtil.errorJson(checkPassword);
        }
        UserInfo userInfoPassword = new UserInfo();
        userInfoPassword.setId(userId);
        userInfoPassword.setUpdateTime(new Date());
        userInfoPassword.setPassword(newPassword);
        if (wxUserService.updateAdminPassword(userInfoPassword)){
            return CommonUtil.successJson("密码修改成功");
        }else{
            return CommonUtil.errorJson("密码修改失败");
        }
    }
}
